<?php
/**
 * File: storage.php
 * Author: Mihaela
 * Date: 11/1/2012
 * Modified By: Brian Gailis
 * Modified Date: 2012-11-07
 * Description: This module has funtions that interact directly with the
 *                database by using the mysqli PHP extension API.
 */

/**
 * Inserts internship record in the internship table in the database.
 *   The first field is configured for PKID, auto increment and
 *   is the key used to uniquely identify each internship record.
 * @param string $internshipID
 * @param string $sponsorName
 * @param string $sponsorCity
 * @param string $sponsorState
 * @param string $postingDate
 * @param string $internshipTitle
 * @param string $internshipCategory
 * @param string $isPaid
 */
function insertStudent($sponsorName, $sponsorCity, $sponsorState, $postingDate, $internshipTitle, $internshipCategory, $isPaid)
{
  $conn = connect( ); // open connection
  $query = "INSERT INTO internship VALUES('', '$sponsorName', '$sponsorCity', '$sponsorState', '$postingDate', '$internshipTitle', '$internshipCategory', '$isPaid')";
  $resource = mysqli_query($conn, $query);
  if ($resource == false) {
    exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn); //close connection
}


/**
 * Queries the database to get all the internships.
 * @return indexed array of associative arrays, where inner associative arrays
 *   hold internship records with  as 'internshipID', 'sponsorName', 'sponsorCity', 'sponsorState', 'postingDate', 'internshipTitle', 'internshipCategory', 'isPaid'
 *   keys.
 */
function selectAllInternships( )
{
  $internships = "";
  $students = array( );
  $conn = connect( ); //open connection
  $query = "SELECT * FROM internship"; //define $query with query string
  $resource = mysqli_query($conn, $query);
  if ($resource == false) {
    exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn);  //close connection

  for ($row = mysqli_fetch_assoc($resource); $row != NULL; 
       $row = mysqli_fetch_assoc($resource)) {
    $internships[ ] = $row;
 }
 return $internships;
}

//search function
function selectSearchedInternships($searchField, $searchFor, $outerSort, $innerSort, $howMany)
{

  $students = array( );
  $conn = connect( ); //open connection

  $query = "SELECT *
              FROM internship 
                WHERE $searchField 
                  LIKE '%$searchFor%'
           ORDER BY $outerSort, $innerSort
           LIMIT $howMany";

  $resource = mysqli_query($conn, $query);
  if ($resource == false) {
    exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn);  //close connection

  for ($row = mysqli_fetch_assoc($resource); $row != NULL; 
       $row = mysqli_fetch_assoc($resource)) {
    $internships[ ] = $row;
 }
 return $internships;
}

/**
 * edit selected internship record in database
 *  by using the internshipID as the where clause
 * @param string $internshipID
 * @return string returns $internships array
 */
function editInternship($internshipID, $sponsorName, $sponsorCity, $sponsorState, $postingDate, $internshipTitle, $internshipCategory, $isPaid)
{
  $conn = connect( ); // open connection
  $query = "UPDATE internship 
              SET internshipID = '$internshipID', sponsorName= '$sponsorName', sponsorCity = '$sponsorCity', 
                  sponsorState = '$sponsorState', postingDate = '$postingDate', internshipTitle = '$internshipTitle', 
                  internshipCategory = '$internshipCategory', isPaid = '$isPaid'
              WHERE internshipID = '$internshipID'"; // edits internship

  $resouce = mysqli_query($conn, $query);
  if ($resouce == false) {
    exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn); // close connection
}


/**
 * Deletes selected internship record from database
 *   by using the internshipID as the where clause
 * @param string $internshipID
 * @return string returns $students array
 */
function deleteInternship($internshipID)
{
  $conn = connect( ); //open connection
  $query = "DELETE FROM internship WHERE internshipID = '$internshipID'"; 
  $resource = mysqli_query($conn, $query); 
  if ($resource == false) {
    exit("Database error: $query<br />" . mysqli_error($conn));
  }
  mysqli_close($conn);  //close connection
}

